package com.int_tech.newTimeTask;

import com.int_tech.MessageCodeAll.dao.XiecheTimesDao;
import com.int_tech.MessageCodeAll.entity.XiecheTimes;
import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import com.int_tech.utils.DateTool;
import com.int_tech.utils.TimesPlus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by liubochao on 2018/6/29.
 */
@Component
public class CheckXiecheGongshi {

    @Autowired
    private MessionDao messionDao;

    @Autowired
    private XiecheTimesDao xiecheTimesDao;

    /**
     * 卸车工时检验
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void checkXiecheGongshi() throws ParseException {

//        List<Mession> messionList = messionDao.findByIsAnsyCarTimeAndIsOverAndCarNumber(1,1,"043C4C155");

        List<Mession> messionList = messionDao.findByIsAnsyCarTimeAndIsOver(1,1);

        for (Mession mession : messionList)
        {
            try {
                mession.setIsAnsyCarTime(2);
                messionDao.save(mession);

                //卸车开始时间
                Date endOpenTime = mession.getEndOpenDoorTime();
                //卸车结束时间
                Date endCloseTime = mession.getEndCloseDoorTime();

                //各个时间字符串日期
                String endOpenDay = DateTool.Date2String(endOpenTime,"yyyy-MM-dd");
                String endCloseDay = DateTool.Date2String(endCloseTime,"yyyy-MM-dd");


                //装车工时如果在同一天
                if(endOpenDay.equals(endCloseDay))
                {
                    //直接添加工时
                    List<Mession> messions = messionDao.findByStartOpenDoorTime(endOpenTime);
                    Mession thisMession = new Mession();

                    if(messions == null || messions.size() == 0)
                    {
                        thisMession = null;
                    }
                    else
                    {
                        thisMession = messions.get(0);
                    }

                    XiecheTimes xiecheTimes = new XiecheTimes();

                    float plus = 0;

                    if(thisMession == null)
                    {
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(mession.getEndCloseDoorTime(),mession.getEndOpenDoorTime(),1));
                        //直接添加工时
                    }
                    else
                    {
                        //工时减半
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(mession.getEndCloseDoorTime(),mession.getEndOpenDoorTime(),2));
                    }

                    xiecheTimes.setCarNumber(mession.getCarNumber());
                    xiecheTimes.setCreatedTime(new Date());
                    xiecheTimes.setDateString(endOpenDay);
                    xiecheTimes.setMessionNumber(mession.getMessionNumber());
                    xiecheTimes.setEndOpenDoorTime(mession.getEndOpenDoorTime());
                    xiecheTimes.setEndCloseDoorTime(mession.getEndCloseDoorTime());
                    xiecheTimes.setStatus(0);

                    xiecheTimes.setOffCarTime(plus);

                    xiecheTimesDao.save(xiecheTimes);
                }
                else
                {
                    //卸车时间跨天了，进行跨天处理
                    List<Mession> messions = messionDao.findByStartOpenDoorTime(endOpenTime);
                    Mession thisMession = new Mession();

                    if(messions == null || messions.size() == 0)
                    {
                        thisMession = null;
                    }
                    else
                    {
                        thisMession = messions.get(0);
                    }

                    XiecheTimes xiecheTimes = new XiecheTimes();

                    String startEndTime = endOpenDay + " 23:59:59";
                    String endStartTIme = endCloseDay + " 00:00:00";

                    Date dStartEndTime = DateTool.string2Date(startEndTime,"yyyy-MM-dd HH:mm:ss");
                    Date dEndStartTime = DateTool.string2Date(endStartTIme,"yyyy-MM-dd HH:mm:ss");

                    float plus = 0;

                    if(thisMession == null)
                    {
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(dStartEndTime,mession.getEndOpenDoorTime(),1));
                        //直接添加工时
                    }
                    else
                    {
                        //工时减半
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(dStartEndTime,mession.getEndOpenDoorTime(),2));
                    }

                    xiecheTimes.setCarNumber(mession.getCarNumber());
                    xiecheTimes.setCreatedTime(new Date());
                    xiecheTimes.setDateString(endOpenDay);
                    xiecheTimes.setMessionNumber(mession.getMessionNumber());
                    xiecheTimes.setEndOpenDoorTime(mession.getEndOpenDoorTime());
                    xiecheTimes.setEndCloseDoorTime(mession.getEndCloseDoorTime());
                    xiecheTimes.setStatus(0);

                    xiecheTimes.setOffCarTime(plus);

                    xiecheTimesDao.save(xiecheTimes);


                    //第二天工时
                    xiecheTimes = new XiecheTimes();

                    plus = 0;

                    if(mession == null)
                    {
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(mession.getEndCloseDoorTime(),dEndStartTime,1));
                        //直接添加工时
                    }
                    else
                    {
                        //工时减半
                        plus = Float.parseFloat(TimesPlus.gongshiTimePlus(mession.getEndCloseDoorTime(),dEndStartTime,2));
                    }

                    xiecheTimes.setCarNumber(mession.getCarNumber());
                    xiecheTimes.setCreatedTime(new Date());
                    xiecheTimes.setDateString(endCloseDay);
                    xiecheTimes.setMessionNumber(mession.getMessionNumber());
                    xiecheTimes.setEndOpenDoorTime(mession.getEndOpenDoorTime());
                    xiecheTimes.setEndCloseDoorTime(mession.getEndCloseDoorTime());
                    xiecheTimes.setStatus(0);

                    xiecheTimes.setOffCarTime(plus);

                    xiecheTimesDao.save(xiecheTimes);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();

                //装车开关门工时计算异常
                mession.setIsAnsyCarTime(-2);
                messionDao.save(mession);
            }
        }
    }
}
